//
//  MessageTableViewCell.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/10.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "MessageTableViewCell.h"

@interface MessageTableViewCell ()
@property (nonatomic, strong)UIView * whiteBGView;
@property (nonatomic, strong)UILabel * titleLab;
@property (nonatomic, strong)UILabel * timeLab;
@property (nonatomic, strong)UILabel * contentLab;
@property (nonatomic, strong)UILabel * orderIDLab;//订单编号
@end

@implementation MessageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        __weak typeof (self)weakSelf = self;
        self.contentView.backgroundColor = getColor(mainColor);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.whiteBGView];
        [self.whiteBGView addSubview:self.titleLab];
        [self.whiteBGView addSubview:self.timeLab];
        [self.whiteBGView addSubview:self.contentLab];
        [self.whiteBGView addSubview:self.orderIDLab];

        [self.whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(10/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(10/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-10/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.contentView.mas_bottom);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.whiteBGView.mas_top).mas_offset(21/WIDTH_6_SCALE);
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(18/WIDTH_6_SCALE);
        }];
        [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.whiteBGView.mas_right).mas_offset(-18/WIDTH_6_SCALE);
            make.centerY.equalTo(weakSelf.titleLab.mas_centerY);
        }];
        [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab.mas_left);
            make.top.equalTo(weakSelf.titleLab.mas_bottom).mas_offset(16/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.timeLab.mas_right);
        }];
        [self.orderIDLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentLab.mas_bottom).mas_offset(20/WIDTH_6_SCALE);
            make.left.equalTo(weakSelf.titleLab.mas_left);
            make.bottom.equalTo(weakSelf.whiteBGView.mas_bottom).mas_offset(-10/WIDTH_6_SCALE);
        }];
    }return self;
}

- (UIView *)whiteBGView{
    if (!_whiteBGView) {
        _whiteBGView = [[UIView alloc] init];
        _whiteBGView.backgroundColor= [UIColor whiteColor];
    }return _whiteBGView;
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = DEF_FontSize_16;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.text = @"消息标题";
    }return _titleLab;
}
- (UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [[UILabel alloc] init];
        _timeLab.font = DEF_FontSize_14;
        _timeLab.textColor = getColor(textGrayColor);
        _timeLab.textAlignment = NSTextAlignmentRight;
        _timeLab.text = @"时间";
    }return _timeLab;
}
- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] init];
        _contentLab.font = DEF_FontSize_14;
        _contentLab.textColor = [UIColor blackColor];
        _contentLab.textAlignment = NSTextAlignmentLeft;
        _contentLab.text = @"内容";
        _contentLab.lineBreakMode = 0;
        _contentLab.numberOfLines = 0;
    }return _contentLab;
}
- (UILabel *)orderIDLab{
    if (!_orderIDLab) {
        _orderIDLab = [[UILabel alloc] init];
        _orderIDLab.font = DEF_FontSize_14;
        _orderIDLab.textColor = [UIColor blackColor];
        _orderIDLab.textAlignment = NSTextAlignmentLeft;
        _orderIDLab.text = [NSString stringWithFormat:@"订单编号:%@", @"---"];
    }return _orderIDLab;
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.titleLab.text = model[@"title"];
    self.contentLab.text = model[@"content"];
    self.orderIDLab.text = [NSString stringWithFormat:@"订单编号:%@", model[@"ordernum"]];
    self.timeLab.text = [self ConvertStrToTime:model[@"createtime"]];;
}
//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr{
    long long time=[timeStr longLongValue];
    //    如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //    long long time=[timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
}
@end
